import _ from 'lodash'
import {toRaw} from 'vue'
import VJsonContext from "./VJsonContext"
import ValidateDialog from "../components/web/ValidateDialog.vue"
import {ElMessage} from 'element-plus'

function getCookie(cname) {
    const name = cname + "=";
    const ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        const c = ca[i].trim();
        if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
    }
    return "";
}

export default {
    props: {
        _input: {type: Object as any, required: false,},
    },
    emits: [
        'ok', 'closeDialog'
    ],
    data() {
        // 创建组件上下文
        const vcxt = VJsonContext.createRuntime(this)
        const _absPath = this._.type.__file
        let _relPath = ''

        // 所有 $prop 属性都会被复制到 data 中
        let propData = {}
        _.forOwn(this.$props._input, (v, k) => {
            if (k.startsWith('_'))
                return
            propData[k] = v
        })

        const idx = _absPath?.indexOf('/src/pages')
        if (idx > 0) {
            _relPath = _absPath.substring(idx + 4)
        }

        return {
            _relPath, _absPath, vcxt,
            input: _.cloneDeep(this.$props?._input || {}),
            _innerForms: [],
            footerDefine: undefined,
            ...propData,
            vjsonRaw: null,
        }
    },
    created() {
        console.time(this._relPath)

        if (typeof this.onCreated === 'function') {
            this.onCreated(this)
        }
    },
    mounted() {
        if (typeof this.onMounted === 'function') {
            this.onMounted(this)
        }
        this.$nextTick(() => {
            console.timeEnd(this._relPath)
        })
        window['cp'] = this

        this.vjsonRaw = _.cloneDeep(this.vjson)
    },
    unmounted() {
        if (typeof this.onUnmounted === 'function') {
            this.onUnmounted(this)
        }
    },
    computed: {},
    methods: {
        ok(result) {
            this.$emit('ok', result)
        },
        close() {
            this.$emit('closeDialog')
        },
        setTitle(result) {
            this.$emit('setTitle', result)
        },

        _registeForm(formInstance) {
            this._innerForms.push(formInstance)
        },
        _unregForm(formInstance) {
            _.remove(this._innerForms, i => i === formInstance)
        },

        scrollToField(field) {
            _.forEach(this._innerForms, form => {
                form.scrollToField(field)
            })
        },

        /**
         * 校验所有窗口
         */
        validateForms() {
            return new Promise((resolve, reject) => {
                const cl = system.showLoadingIfButton()
                Promise.all(_.map(this._innerForms, r => r.validate()))
                    .then(res => {
                        resolve(res)

                    })
                    .catch((...validRetList) => {
                        system.pushCmpWithId(ValidateDialog, {
                            scope: this,
                            validRetList
                        })
                        reject(validRetList)

                    })
                    .finally(() => {
                        if (cl) {
                            cl()
                        }
                    })
            })
        },

        clearForms() {
            _.forEach(this._innerForms, f => {
                f.resetFields()
            })
        }
    }
}